<!--
  * 图标地点
  *
  * @Author:    骆伟林
  * @Date:      2025-10-27 11:46:41
  * @Copyright  软件需求工程第八组
-->
<template>
  <a-modal :title="'添加'" :width="800" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <a-form-item label="图标地点名称" name="markerName">
        <a-input style="width: 100%" v-model:value="form.markerName" placeholder="图标地点名称" />
      </a-form-item>
      <a-form-item label="图标地点简介" name="markerIntroduction">
        <a-input style="width: 100%" v-model:value="form.markerIntroduction" placeholder="图标地点简介" />
      </a-form-item>
      <a-form-item label="图标地点类型" name="markerType">
        <DictSelect width="100%" v-model:value="form.markerType" :dict-code="DICT_CODE_ENUM.MARKE_TYPE || ''" placeholder="图标地点类型" />
      </a-form-item>
      <a-form-item label="图标地点评分" name="markerRating">
        <a-rate v-model:value="form.markerRating" />
      </a-form-item>
      <a-form-item label="图标地点营业时间" name="markerOpeningHours">
        <a-input style="width: 100%" v-model:value="form.markerOpeningHours" placeholder="图标地点营业时间" />
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick, onMounted } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { DICT_CODE_ENUM } from '/@/constants/support/dict-const.js';
  import DictSelect from '/@/components/support/dict-select/index.vue';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList', 'submit']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    console.log('rowData', rowData);

    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    form.markerLatitude = rowData.latitude;
    form.markerLongitude = rowData.longitude;
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    markerId: undefined, //图标地点id
    addressId: undefined, //地点id
    markerLongitude: undefined, //图标地点经度
    markerLatitude: undefined, //图标地点维度
    markerName: undefined, //图标地点名称
    markerIntroduction: undefined, //图标地点简介
    markerType: undefined, //图标地点类型
    markerRating: undefined, //图标地点评分
    markerOpeningHours: undefined, //图标地点营业时间
  };

  let form = reactive({ ...formDefault });

  const rules = {
    markerId: [{ required: true, message: '图标地点id 必填' }],
    addressId: [{ required: true, message: '地点id必填' }],
    markerLongitude: [{ required: true, message: '图标地点经度 必填' }],
    markerLatitude: [{ required: true, message: '图标地点维度 必填' }],
    markerName: [{ required: true, message: '图标地点名称 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    emits('submit', form);
    onClose();
  }

  defineExpose({
    show,
  });
</script>
